from machine import Pin,I2C,RTC,ADC
import zlib
import machine
SHT31_address       =   68
LADD=60
i2c = I2C(1,scl=Pin(18), sda=Pin(19),freq=400000)
adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)    # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
adc.width(ADC.WIDTH_12BIT)  
rtc=RTC()
def LC(buf):
    i2c.writeto(LADD,b'\x80'+buf)
def LDS(buf):
    i2c.writeto(LADD,b'\x40'+buf)
def LD(buf):
    i2c.writeto(LADD,b'\xc0'+buf)
def lcdAddress(x,y,x_total,y_total):
    LC(b"\x15")
    LD(x.to_bytes(1,"big"))
    LD((x+x_total-1).to_bytes(1,"big"))
    LC(b"\x30")
    LC(b"\x75")
    LD(y.to_bytes(1,"big"))
    LD((y+y_total-1).to_bytes(1,"big"))
    LC(b"\x30")
    LC(b"\x5c")
def lprint(strs,x=0,y=0):
    lcdAddress(x*6,y,len(strs)*6,8)
    for i in strs:
        LDS(dd[(ord(i)-32)*5:(ord(i)-31)*5]+b'\x00')
def print32(strs,x=0,y=0):
    n=0
    for i in strs:
        lcdAddress(x+n*16,y,16,32)
        b=3291+(ord(i)-45)*64
        LDS(dd[b:b+64])
        n=n+1

def st1(t,n):
    lcdAddress(n*32,0,32,64)
    b=475+t*256
    LDS(dd[b:b+256])

i2c.writeto(SHT31_address,b"\x2c\x06")
dd=zlib.decompress(rtc.memory())
print('sf')
da=i2c.readfrom_mem(104,0,3)
s=((da[0]>>4) * 10) + (da[0]&0x0f)
m1=da[1]>>4
m2=da[1]&0x0f
h1=da[2]>>4
h2=da[2]&0x0f
st1(m2,4)
if(m2==0):
    st1(m1,3)
    if(m1==0):
        st1(h1,0)
        st1(h2,1)
        da=i2c.readfrom_mem(104,3,4)
        d=((da[1]>>4) * 10) + (da[1]&0x0f)
        w=((da[0]>>4) * 10) + (da[0]&0x0f)
        mo=((da[2]>>4) * 10) + (da[2]&0x0f)
        y=((da[3]>>4) * 10) + (da[3]&0x0f)
        lprint("20%02d-%02d-%02d"%(y,mo,d),28,2)
        lcdAddress(170+32,4,16,16)
        d=4379+(w%7)*32
        LDS(dd[d:d+32])

v=adc.read()
print32("%.2f<"%(v*7.36/4096),0,12)
raw=i2c.readfrom(SHT31_address,6)
(t,h)=(raw[0] << 8) + raw[1], (raw[3] << 8) + raw[4]
shtrt = -45 + (175 * (t / 65535))
shtrh = 100 * (h / 65535)
print32("%.2f;/%.2f=///"%(shtrt,shtrh),0,8)
machine.deepsleep((60-s)*1000)

